{
 "cells": [
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-26T06:11:45.624938Z",
     "start_time": "2025-06-26T06:11:45.606617Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 多通道\n",
    "import torch\n",
    "from d2l import torch as d2l"
   ],
   "id": "d8d2dfe45b95a2cb",
   "outputs": [],
   "execution_count": 18
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-26T06:11:45.655819Z",
     "start_time": "2025-06-26T06:11:45.641998Z"
    }
   },
   "cell_type": "code",
   "source": [
    "def corr2d_multi_in(X, K):\n",
    "    # 先遍历“X”和“K”的第0个维度（通道维度），再把它们加在一起\n",
    "    return sum(d2l.corr2d(x, k) for x, k in zip(X, K))"
   ],
   "id": "17c43ecfe85e49e5",
   "outputs": [],
   "execution_count": 19
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-26T06:11:45.686410Z",
     "start_time": "2025-06-26T06:11:45.672678Z"
    }
   },
   "cell_type": "code",
   "source": [
    "X = torch.tensor([[[0.0, 1.0, 2.0],\n",
    "\t\t\t\t   [3.0, 4.0, 5.0],\n",
    "\t\t\t\t   [6.0, 7.0, 8.0]],\n",
    "               [[1.0, 2.0, 3.0],\n",
    "\t\t\t\t[4.0, 5.0, 6.0],\n",
    "\t\t\t\t[7.0, 8.0, 9.0]]]) #2*3*4\n",
    "K = torch.tensor([[[0.0, 1.0],\n",
    "\t\t\t\t   [2.0, 3.0]],\n",
    "\t\t\t\t  [[1.0, 2.0],\n",
    "\t\t\t\t   [3.0, 4.0]]])\n",
    "\n",
    "corr2d_multi_in(X, K)"
   ],
   "id": "1a851db9280213b3",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[ 56.,  72.],\n",
       "        [104., 120.]])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 20
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-26T06:11:45.748389Z",
     "start_time": "2025-06-26T06:11:45.734373Z"
    }
   },
   "cell_type": "code",
   "source": [
    "def corr2d_multi_in_out(X, K):\n",
    "    # 迭代“K”的第0个维度，每次都对输入“X”执行互相关运算。\n",
    "    # 最后将所有结果都叠加在一起\n",
    "    return torch.stack([corr2d_multi_in(X, k) for k in K], 0)"
   ],
   "id": "9ed7025438f56f23",
   "outputs": [],
   "execution_count": 21
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-26T06:11:45.903136Z",
     "start_time": "2025-06-26T06:11:45.889043Z"
    }
   },
   "cell_type": "code",
   "source": [
    "K = torch.stack((K, K + 1, K + 2), 0)\n",
    "K.shape"
   ],
   "id": "61bd92e0075469e1",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([3, 2, 2, 2])"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 22
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-26T06:11:45.949626Z",
     "start_time": "2025-06-26T06:11:45.933810Z"
    }
   },
   "cell_type": "code",
   "source": "corr2d_multi_in_out(X, K)",
   "id": "956ac4b59e53bad3",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[ 56.,  72.],\n",
       "         [104., 120.]],\n",
       "\n",
       "        [[ 76., 100.],\n",
       "         [148., 172.]],\n",
       "\n",
       "        [[ 96., 128.],\n",
       "         [192., 224.]]])"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 23
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-26T06:12:18.406892Z",
     "start_time": "2025-06-26T06:12:18.394798Z"
    }
   },
   "cell_type": "code",
   "source": [
    "def corr2d_multi_in_out_1x1(X, K):\n",
    "    c_i, h, w = X.shape\n",
    "    c_o = K.shape[0]\n",
    "    X = X.reshape((c_i, h * w))\n",
    "    K = K.reshape((c_o, c_i))\n",
    "    # 全连接层中的矩阵乘法\n",
    "    Y = torch.matmul(K, X)\n",
    "    return Y.reshape((c_o, h, w))"
   ],
   "id": "a3fc39dad3a1776c",
   "outputs": [],
   "execution_count": 28
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-26T06:12:19.320925Z",
     "start_time": "2025-06-26T06:12:19.300384Z"
    }
   },
   "cell_type": "code",
   "source": [
    "X = torch.normal(0, 1, (3, 3, 3))\n",
    "K = torch.normal(0, 1, (2, 3, 1, 1))\n",
    "\n",
    "Y1 = corr2d_multi_in_out_1x1(X, K)\n",
    "Y2 = corr2d_multi_in_out(X, K)\n",
    "assert float(torch.abs(Y1 - Y2).sum()) < 1e-6"
   ],
   "id": "30556f3850db67a4",
   "outputs": [],
   "execution_count": 29
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-26T06:15:07.616682Z",
     "start_time": "2025-06-26T06:15:07.603114Z"
    }
   },
   "cell_type": "code",
   "source": "Y1,Y2",
   "id": "c171d615d8ae8448",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor([[[-1.3241,  3.1862, -0.0984],\n",
       "          [ 3.2378, -1.4321, -1.7172],\n",
       "          [-3.8280, -0.7273,  0.7941]],\n",
       " \n",
       "         [[ 1.5082,  0.2228, -1.1251],\n",
       "          [-2.4253,  0.9448, -3.7244],\n",
       "          [ 2.7265, -2.2066,  1.8052]]]),\n",
       " tensor([[[-1.3241,  3.1862, -0.0984],\n",
       "          [ 3.2378, -1.4321, -1.7172],\n",
       "          [-3.8280, -0.7273,  0.7941]],\n",
       " \n",
       "         [[ 1.5082,  0.2228, -1.1251],\n",
       "          [-2.4253,  0.9448, -3.7244],\n",
       "          [ 2.7265, -2.2066,  1.8052]]]))"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 30
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": "",
   "id": "48d197f724f46846"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
